package gov.gfbzb.advice.serviceImpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hankcs.hanlp.HanLP;
import gov.gfbzb.advice.controller.dto.KeyValueDTO;
import gov.gfbzb.advice.entity.Advice;
import gov.gfbzb.advice.service.IAdviceService;
import gov.gfbzb.advice.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Value("${gfbzb.env}")
    private String env;

    @Autowired
    private IAdviceService adviceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Map<String, Integer> getKeyWord() {
        Map<String, Integer>  keyWords = (Map<String, Integer> ) redisTemplate.opsForValue().get(env + "keyWord");
        if (keyWords!=null){
            return keyWords ;
        }
        return new HashMap<>();
    }

    @Override
    public List<KeyValueDTO> getCountyQuestionCount(Boolean isPass) {
        // 各区县咨询总问题量
        if (isPass==null){
            return (List<KeyValueDTO>)redisTemplate.opsForValue().get(env+"CountyQuestionAllCount");
        }
        // 各区县咨询已审核问题量
        if (isPass){
            return (List<KeyValueDTO>)redisTemplate.opsForValue().get(env+"CountyPassQuestionCount");
        }
        // 各区县咨询待审核问题量
        return (List<KeyValueDTO>)redisTemplate.opsForValue().get(env+"CountyNoPassQuestionCount");
    }

    @Override
    public Long getAllQuestionCount(Boolean isPass) {
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        if (isPass!=null){
            // 已审核
            if (isPass){
                queryWrapper.eq("question_status",1);
            }else {
                // 待审核、已驳回
                queryWrapper.and(wrapper -> {
                    wrapper.eq("question_status", 2)
                            .or()
                            .eq("question_status", 0);
                });
            }
        }
        return adviceService.count(queryWrapper);
    }

    @Override
    public Long getDayQuestionAllCount() {
        // 获取当日总问题量
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("is_delete", 0)

                .ge("create_time", DateUtil.beginOfDay(new Date()))
                .le("create_time", DateUtil.endOfDay(new Date()));
        return adviceService.count(queryWrapper);
    }

    @Override
    public Long getWeekQuestionAllCount() {
        //  周总问题量 往前推6天 的0点
        // 获取7天前的开始时间和今天的结束时间
        Date weekStart = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -6));
        Date dateEnd = DateUtil.endOfDay(new Date());
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0)
                .ge("create_time", weekStart)
                .le("create_time", dateEnd);
        return adviceService.count(queryWrapper);
    }

    @Override
    public Long getMonthQuestionAllCount() {
        Date monthStart = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30));
        Date dateEnd = DateUtil.endOfDay(new Date());
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0)
                .ge("create_time", monthStart)
                .le("create_time", dateEnd);
        return adviceService.count(queryWrapper);
    }

}
